//
//  MYGApplyFriendViewController.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/4/26.
//

import UIKit

class MYGApplyFriendViewController: MYGBaseController {
   
    var model:MYGSysMessageModel? {
        didSet{
            guard let item = model else {
                return
            }
            
            avar.sd_setImage(with: URL(string: "\(item.from_doctor.avatar)"), placeholderImage: UIImage(named: "heade"))
            nameLabel.text = "\(item.from_doctor.name)  \(item.from_doctor.department)"
            timelabel.text = item.updated_at
            hospitalLabel.text = item.from_doctor.hospital
            contentlabel.text = item.message
                        
        }
        
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "好友申请"
        configUI()
    }
    
    func configUI(){
        view.addSubview(avar)
        view.addSubview(nameLabel)
        view.addSubview(timelabel)
        view.addSubview(hospitalLabel)
        view.addSubview(label)
        view.addSubview(contentlabel)
        view.addSubview(agreenButton)
        view.addSubview(reButton)
        
        avar.snp.makeConstraints { make in
            make.left.top.equalToSuperview().inset(22.5)
            make.height.width.equalTo(45)
        }
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(avar.snp.right).offset(11.5)
            make.top.equalTo(avar)
            make.height.equalTo(24)
        }
        timelabel.snp.makeConstraints { make in
            make.centerY.equalTo(nameLabel)
            make.right.equalToSuperview().inset(19.5)
        }
        hospitalLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(4)
            make.height.equalTo(18.5)
        }
        label.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(hospitalLabel.snp.bottom).offset(6)
            make.height.equalTo(18.5)
        }
        contentlabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(hospitalLabel.snp.bottom).offset(10)
        }
        agreenButton.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(30)
            make.top.equalTo(contentlabel.snp.bottom).offset(17.5)
            make.width.equalTo(64)
            make.height.equalTo(21)
        }
        reButton.snp.makeConstraints { make in
            make.right.equalTo(agreenButton.snp.left).offset(-16.5)
            make.top.equalTo(contentlabel.snp.bottom).offset(17.5)
            make.width.equalTo(64)
            make.height.equalTo(21)
        }
        
    }
    
    // MARK: - lazy load
    private lazy var avar: UIImageView = {
        let imgV = UIImageView()
        imgV.image = UIImage(named: "head")
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 22.5
        return imgV
    }()
    
    lazy var nameLabel: UILabel = {
       let label = UILabel()
       label.font = .medium(15)
       label.textColor = .rgba(44, 44, 44, 1)
       label.text = "申乔淑琴 主治医师"
       return label
   }()
   
    lazy var timelabel: UILabel = {
       let label = UILabel()
       label.font = .regular(13)
        label.textAlignment = .right
       label.textColor = .rgba(128, 128, 128, 1)
       label.text = "09:50"
       return label
   }()
   
    
    lazy var hospitalLabel: UILabel = {
       let label = UILabel()
        label.font = .regular(13)
        label.textColor = .rgba(128, 128, 128, 1)
       label.text = "北京安贞医院"
       return label
   }()
   
    lazy var label: UILabel = {
       let label = UILabel()
       label.font = .medium(1)
       label.textColor = .rgba(44, 44, 44, 1)
       label.text = "申请添加您为好友"
       return label
   }()
   
    lazy var contentlabel: UILabel = {
       let label = UILabel()
       label.font = .regular(13)
       label.textColor = .rgba(150, 150, 150, 1)
       label.text = "申我是某某某某我是某某某某我是某某某某是某"
       return label
   }()
   
    
    private lazy var agreenButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("同意", for: .normal)
        btn.setTitleColor(.rgba(255, 255, 255, 1), for: .normal)
        btn.titleLabel?.font = .regular(13)
        btn.addTarget(self, action: #selector(clickButton(_ :)), for: .touchUpInside)
        btn.setBackgroundColor(color: .rgba(77, 120, 237, 1), forState: .normal)
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 10
        return btn
    }()
    private lazy var reButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("拒绝", for: .normal)
        btn.setTitleColor(.rgba(151, 151, 151, 1), for: .normal)
        btn.titleLabel?.font = .regular(13)
        btn.addTarget(self, action: #selector(clickButton(_:)), for: .touchUpInside)
        
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 10
        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor.rgba(151, 151, 151, 1).cgColor
        return btn
    }()
    
}
extension MYGApplyFriendViewController {
    
    //同意好友申请
    @objc func clickButton(_ sender: UIButton){
        
        let from_user_id = model?.from_user_id ?? 0
        let to_user_id = model?.to_user_id ?? 0
        let type = model?.type ?? 0
        let sys_message_id = model?.id ?? 0
        if sender == agreenButton {
            MYGLoading.show()
            MYGNetwork<MYGMessageApi,MYGEmptyModel>.request(target: .agreeFriend(from_user_id: from_user_id, to_user_id: to_user_id, type: type,sys_message_id:"\(sys_message_id)")) { _ in
                MYGLoading.hide()
                self.navigationController?.popViewController(animated: true)
            } failed: { _, error in
                MYGLoading.hide()
                MMYGHUD.showToast(text: error)
            }
        }else {
            self.navigationController?.popViewController(animated: true)
        }
        
        
    }
}
